// 第四步，配置条件
var archiveType = "";
var archives =['同库分区','跨库分区','同库全表','垮裤全表'];
// 脚本验证按钮点击事件
$('#button-script-verify').click(function() {
	conditionFormData = $("#conditionForm").form("value");
	//1 条件归档， 2 分区归档， 3 全表归档
	if(conditionFormData.archiveMode == "2" || conditionFormData.archiveMode == "3" ){
		var dbLinkId = $('input[name="dbLinkId"]').attr("vId");
		var dbUserName = $('input[name="dbLinkId"]').val();
		var procedureName = $("input[name='procedureName']").val();
		var tableName = $('input[name="tableName"]').val();
		tableName = $.parseJSON(tableName)[0].value;
		if (archiveType == archives[0] || archiveType == archives[1]) { //主要针对oracle看看是否有分区
			$.post(webRoot + 'common/sysModule/isExistPartitions.do?dbLinkId='+dbLinkId+
					'&dbUserName='+dbUserName+'&tableName='+tableName, function (result) {
				if (result == true || result.success) {
					$('textarea[name="scriptVerify"]').val("源表存在分区,验证通过!");
					scriptVerifyIsOK = true;
				} else {
					$('textarea[name="scriptVerify"]').val(result.errCode+",验证不通过!");
					fish.toast('warn', result.errCode+",验证不通过!");
					scriptVerifyIsOK = false;
				}
			});  
		} else if (archiveType == archives[2]) {//同库全表			
			$.post(webRoot + 'common/sysModule/isExistProcedureInfo.do?dbLinkId='+dbLinkId+
					'&dbUserName='+dbUserName+'&procedureName='+procedureName, function (result) {
				if (result == true || result.success) {
					$('textarea[name="scriptVerify"]').val("数据库存在该存储过程，脚本验证通过");
					scriptVerifyIsOK = true;
				} else {
					$('textarea[name="scriptVerify"]').val("数据库不存在该存储过程,,请使用条件归档!");
					scriptVerifyIsOK = false;
				}
			});  
		} else if (archiveType == archives[3]) {//跨库全表 用导入导出 TODO 暂不做校验
			$('textarea[name="scriptVerify"]').val("跨库全表 用导入导出 暂不做校验!");
			scriptVerifyIsOK = true;
		} else {
			$('textarea[name="scriptVerify"]').val("不同数据库只能用条件归档！");
			scriptVerifyIsOK = false;
		}
	}else{
		if(conditionFormData.selectCondition == null || conditionFormData.selectCondition == ""){
			fish.toast('warn', '请输入归档条件');
			return;
		}
		conditionFormData.dbLinkId= $('input[name="dbLinkId"]').attr("vId");
		conditionFormData.dbUserName = $('input[name="dbLinkId"]').val();
		conditionFormData.tableName = $('input[name="tableName"]').val();
		$.blockUI({message : "数据载入中,请稍候..."});
		$.ajax({
			url : webRoot + 'baseconfig/dmsrctable/scriptVerify.do',
			type : 'POST',
			async : true,
			data : _.extend(srcTableFormData, targetTableFormData, conditionFormData)
		}).done(function(serverData, textStatus, jqXHR) {
			if(serverData.substr(0, 1) == "0"){// 验证通过
				scriptVerifyIsOK = true;
				oldSelectCondition = conditionFormData.selectCondition;
			}else if(serverData.substr(0, 1) == "1"){//  验证不通过
				scriptVerifyIsOK = false;
			}
			$('textarea[name="scriptVerify"]').val(serverData.substr(1, serverData.length - 1));
		}).fail(function() {
			fish.toast('error', '网络连接失败！');
		}).always(function() {
			$.unblockUI();
		});
	}
});

$(document).ready(function() {
	$("#thirdBondRigth,#textArea,#importCommand,#exportCommand,#importCommandArea,#exportCommandArea").hide();
	var availableTags = [
        "Column1",
        "Column2",
        "Column3",
        "Column4",
        "Column5",
        "Column6",
        "Column7",
        "Column8",
        "Column9",
        "Column10",
        "Column11",
        "Column12",
        "Column13"
    ];

    $("#tags").autocomplete({
        source: availableTags
    });
	$("input[name=archiveMode]").click(function(){
		archiveModeShow();
	});

	
//	archiveModeShow();
	
	

//	 var url_='';
//	if(typeof($('input[name="dbLinkId"]').attr("vId")) == "undefined" || typeof($('input[name="dbLinkId"]').val()) == "undefined"){
//			url_ = 'common/sysModule/qryProcedureInfo.do?dbLinkId='+0+'&dbUserName=0';
//	}else{
//			var dbLinkId = $('input[name="dbLinkId"]').attr("vId");
//			url_ = 'common/sysModule/qryProcedureInfo.do?dbLinkId='+dbLinkId+'&dbUserName='+dbUserName;
//	}
//    
//	$.post(webRoot + url_, function (result) {
//        $("input[name='procedureName']").combobox({
//            placeholder: "请选择",
//            dataTextField: 'objectName',
//            dataValueField: 'objectId',
//            dataSource: result
//        });  
//    });     
//
//
//	$('#procedureName').one('combobox:open', function (e) {
//		var dbLinkId = $('input[name="dbLinkId"]').attr("vId");
//		console.log(dbLinkId);
//		$("input[name='procedureName']").val("");
//		$.post(webRoot + url_, function (result) {
//		        $("input[name='procedureName']").combobox({
//		            placeholder: "请选择",
//		            dataTextField: 'objectName',
//		            dataValueField: 'objectId',
//		            dataSource: result
//		        });  
//		    });        
//     });



 //    $combobox1.on('combobox:close', function (e) {
 //        console.log('close event');
 //    });
   
//	// 脚本验证编辑后触发事件
//	$('textarea[name="selectCondition"]').blur(function() {
//		conditionFormData = $("#conditionForm").form("value");
//		if(conditionFormData.selectCondition == null || conditionFormData.selectCondition == ""){
//			fish.showToast('请输入归档条件', {type : 'success'});
//			return;
//		}
//		conditionFormData.dbLinkId= $('input[name="dbLinkId"]').attr("vId");
//		conditionFormData.dbUserName = $('input[name="dbLinkId"]').val();
//		conditionFormData.tableName = $('input[name="tableName"]').val();
//		$.blockUI({message : "数据载入中,请稍候..."});
//		$.ajax({
//			url : webRoot + 'baseconfig/dmsrctable/scriptVerify.do',
//			type : 'POST',
//			async : true,
//			data : _.extend(srcTableFormData, targetTableFormData, conditionFormData)
//		}).done(function(serverData, textStatus, jqXHR) {
//			if(serverData.substr(0, 1) == "0"){// 验证通过
//				scriptVerifyIsOK = true;
//			}else if(serverData.substr(0, 1) == "1"){//  验证不通过
//				scriptVerifyIsOK = false;
//			}
//			$('textarea[name="scriptVerify"]').text(serverData.substr(1, serverData.length - 1));
//		}).fail(function() {
//			fish.showToast('网络连接失败！', {type : 'success'});
//		}).always(function() {
//			$.unblockUI();
//		});
//	});

});

function setSelConditionInfo(dmSrcTablePO){
	// 配置条件界面
	$('textarea[name="selectCondition"]').val(dmSrcTablePO.selectCondition);
	if(dmSrcTablePO.archiveMode == 1){
		$($('input[name="archiveMode"]')[0]).prop('checked',true);
	}else if(dmSrcTablePO.archiveMode == 2){
		$($('input[name="archiveMode"]')[1]).prop('checked',true);
	}else if(dmSrcTablePO.archiveMode == 3){
		$($('input[name="archiveMode"]')[2]).prop('checked',true);
	}
	archiveModeShow();
	$('input[id="procedureName"]').combobox('value',dmSrcTablePO.procedureName+"");//下拉框操作类型赋值
	
	$('input[name="importTableArea"]').val(dmSrcTablePO.importTableArea);
	$('input[name="importCommandArea"]').val(dmSrcTablePO.importCommandArea);
	$('input[name="exportTableArea"]').val(dmSrcTablePO.exportTableArea);
	$('input[name="exportCommandArea"]').val(dmSrcTablePO.exportCommandArea);
	$('input[name="importTable"]').val(dmSrcTablePO.importTable);
	$('input[name="importCommand"]').val(dmSrcTablePO.importCommand);
	$('input[name="exportTable"]').val(dmSrcTablePO.exportTable);
	$('input[name="exportCommand"]').val(dmSrcTablePO.exportCommand);
	
	

}

function archiveModeShow(){// 归档方式
		scriptVerifyIsOK = false;
		$('textarea[name="scriptVerify"]').val("");
		var dbLinkId = $('input[name="dbLinkId"]').attr("vId");
		var dbUserName = $('input[name="dbLinkId"]').val();
		var targetDbLinkId = $('input[name="' + targetTableDbLinkId_name + 1+ '"]').attr("vId");
		var targetDbUserName = $('input[name="' + targetTableDbLinkId_name + 1+ '"]').val();
		var flag;
		var operType = $('input[name="operType"]').val();
		if(targetDbLinkId==dbLinkId && dbUserName == targetDbUserName){			
			$('input[name="dbDataFlag"]').val(0);
			flag = 0;
		}else{
			$('input[name="dbDataFlag"]').val(1);
			flag = 1;
		}
		switch ($("input[name=archiveMode]:checked").attr("value")) {
		case "1":
			if($("input[name=archiveMode]:checked").attr("value") == "1"){// 条件归档
				$("#thirdBondRigth,#textArea,#importCommand,#exportCommand,#importCommandArea,#exportCommandArea").hide();
				$("#scriptVerify,#sectCondition").show();
			}else{
				$("#sectCondition").show();
			}
			break;
		case "2":
			if($("input[name=archiveMode]:checked").attr("value") == "2"){// 分区归档
				
				$("#thirdBondRigth,#scriptVerify,#importCommand,#exportCommand,#importCommand,#exportCommand,#sectCondition").hide();
				$("#scriptVerify").show();
				if(flag==0){
					archiveType = archives[0];
					$("#importCommandArea,#exportCommandArea").hide();
					$("#textArea").show();

				}else{
					archiveType = archives[1];
					$("#importCommandArea,#exportCommandArea").show();
					$("#textArea").hide();

				}

			}else{
				$("#textArea").hide();
			}
			break;
		case "3":
			if($("input[name=archiveMode]:checked").attr("value") == "3"){// 全表归档
				$("#sectCondition,#textArea,#importCommandArea,#exportCommandArea").hide();
				$("#scriptVerify").show();
				if(flag==0){
					archiveType = archives[2];
					$("#importCommand,#exportCommand").hide();
					$("#thirdBondRigth").show();

				}else{
					archiveType = archives[3];
					$("#importCommand,#exportCommand").show();
					$("#thirdBondRigth").hide();
				}
			}else{
				$("#thirdBondRigth").hide();
			}
			var result = [{objectName: "full_backup_transable_variant", objectId: "full_backup_transable_variant"},
			              {objectName: "full_backup_time_variant", objectId: "full_backup_time_variant"}];
//			url_ = 'common/sysModule/qryProcedureInfo.do?dbLinkId='+dbLinkId+'&dbUserName='+dbUserName;
//			$.post(webRoot + url_, function (result) {
		        $("input[name='procedureName']").combobox({
		            placeholder: "请选择",
		            dataTextField: 'objectName',
		            dataValueField: 'objectId',
		            dataSource: result
		        });
//		    });    
			break;
		default:
			break;
		}
	};